package com.youhome.service.service.impl;

import com.youhome.common.exception.CustomException;
import com.youhome.common.response.ResultCode;
import com.youhome.service.mapper.BRecommendHouseMapper;
import com.youhome.service.model.BCompany;
import com.youhome.service.model.BRecommendHouse;
import com.youhome.service.model.ListHubList;
import com.youhome.service.model.Principal;
import com.youhome.service.service.BCompanyService;
import com.youhome.service.service.BRecommendHouseService;
import com.youhome.service.service.ListHubService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * Created by chen365 on 2017/11/17.
 */
@Service
public class BRecommendHouseServiceImpl implements BRecommendHouseService {
    @Autowired
    private BRecommendHouseMapper bRecommendHouseMapper;
    @Autowired
    private BCompanyService bCompanyService;
    @Autowired
    private ListHubService listHubService;

    @Override
    public int delete(Long companyId, String houseId) {
        BRecommendHouse record = new BRecommendHouse();
        record.setCompanyId(companyId);
        record.setHouseId(houseId);
        return bRecommendHouseMapper.delete(record);
    }

    @Override
    public int insert(BRecommendHouse record) {
        return bRecommendHouseMapper.insert(record);
    }

    @Override
    public Boolean selectIsExistByCompanyIdAndHouseID(BRecommendHouse record) {
        return bRecommendHouseMapper.selectIsExistByCompanyIdAndHouseID(record) > 0 ? true : false;
    }

    @Override
    public List<Long> selectHouseIDByCompanyId(Long companyId) {
        return bRecommendHouseMapper.selectHouseIDByCompanyId(companyId);
    }

    @Override
    public int sort(Long companyId, String houseId, Integer sort) {
        BRecommendHouse record = new BRecommendHouse();
        record.setCompanyId(companyId);
        record.setHouseId(houseId);
        record.setSort(sort);
        return bRecommendHouseMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int delete(Long companyId) {
        return bRecommendHouseMapper.deleteByCompanyId(companyId);
    }

    @Override
    public int selectCountByCompanyId(Long companyId) {
        return bRecommendHouseMapper.selectCountByCompanyId(companyId);
    }

    @Override
    public List<ListHubList> selectRecommendHouseByCompanyId(String uuid) throws CustomException {

        if (StringUtils.isEmpty(uuid)) {
            throw new CustomException(ResultCode.ERROR_PARAMETER);
        }
        BCompany company = bCompanyService.selectByUUID(uuid);
        if (company == null) {
            throw new CustomException(ResultCode.DATA_DOES_NOT_EXIST_OR_HAS_BEEN_DELETED);
        }
        List<Long> houseIds = bRecommendHouseMapper.selectHouseIDByCompanyId(company.getCompanyId());
        if (houseIds.size() > 0) {
            List<ListHubList> list = listHubService.selectRecommendHouseByCompanyId(company.getCompanyId());
            if (list.size() > 0) {
                return list;
            } else {
                //默认推荐房源
            }
        } else {
//                MSearchParam param = new MSearchParam();
//                List<String> status = new ArrayList<>();
//                status.add("Active");
//                param.setStatus(status);
//                PageInfo<MHouseSearch> houseList = mHouseSearchService.selectByBean(pageNumber, pageSize, param);
//                throw new CustomExceptionPage(ResultCode.SUCCESS, houseList);
        }

        return null;
    }


    /**
     * 查询推荐房源数量
     *
     * @param principal
     * @return 数量
     * @throws CustomException
     */
    @Override
    public int selectRecommendHouseCount(Principal principal) throws CustomException {
        Long userId = principal.getUser().getUserId();
        BCompany company = bCompanyService.selectByUserId(userId);
        if (company == null) {
            throw new CustomException(ResultCode.DATA_DOES_NOT_EXIST_OR_HAS_BEEN_DELETED);
        }
        return bRecommendHouseMapper.selectCountByCompanyId(company.getCompanyId());
    }


    /**
     * 删除所有推荐房源
     *
     * @param principal
     * @throws CustomException
     */
    @Override
    public void deleteRecommendHouseByAll(Principal principal) throws CustomException {
        Long userId = principal.getUser().getUserId();
        BCompany company = bCompanyService.selectByUserId(userId);
        if (company == null) {
            throw new CustomException(ResultCode.DATA_DOES_NOT_EXIST_OR_HAS_BEEN_DELETED);
        }
        if (bRecommendHouseMapper.deleteByCompanyId(company.getCompanyId()) == 0) {
            throw new CustomException(ResultCode.FAIL);
        }
    }

    /**
     * 删除推荐房源
     *
     * @param principal
     * @param houseId   房源ID
     * @throws CustomException
     */
    @Override
    public void deleteRecommendHouseByHouseId(Principal principal, String houseId) throws CustomException {
        Long userId = principal.getUser().getUserId();
        BCompany company = bCompanyService.selectByUserId(userId);
        if (company == null) {
            throw new CustomException(ResultCode.DATA_DOES_NOT_EXIST_OR_HAS_BEEN_DELETED);
        }
        BRecommendHouse record = new BRecommendHouse();
        record.setCompanyId(company.getCompanyId());
        record.setHouseId(houseId);
        if (bRecommendHouseMapper.delete(record) == 0) {
            throw new CustomException(ResultCode.FAIL);
        }
    }

    /***
     * 推荐房源
     * @param houseId 房源ID
     * @throws CustomException
     */
    @Override
    public void recommendHouse(Principal principal,String houseId) throws CustomException {
        Long userId = principal.getUser().getUserId();

        if (StringUtils.isEmpty(houseId)) {
            throw new CustomException(ResultCode.EMPTY_HOUSE_ID);
        }

        BCompany company = bCompanyService.selectByUserId(userId);
        if (company == null) {
            throw new CustomException(ResultCode.DATA_DOES_NOT_EXIST_OR_HAS_BEEN_DELETED);
        }

        BRecommendHouse recommendHouse = new BRecommendHouse();
        recommendHouse.setCompanyId(company.getCompanyId());
        recommendHouse.setHouseId(houseId);
        recommendHouse.setStatus(1);
        recommendHouse.setSort(0);
        recommendHouse.setCtime(new Date());

        int count = selectCountByCompanyId(company.getCompanyId());
        if (count >= 10) {
            throw new CustomException(ResultCode.RECOMMEND_HOUSE_COUNT_EXCEED_CAPACITY);
        }

        if (selectIsExistByCompanyIdAndHouseID(recommendHouse)) {
            throw new CustomException(ResultCode.HOUSE_HAVE_RECOMMENDED);
        }

        if (bRecommendHouseMapper.insert(recommendHouse) == 0) {
            throw new CustomException(ResultCode.FAIL);
        }
    }

}
